import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode:'history',
  routes:[
    {path: '/login',name: 'login',component: () => import('./views/Login.vue')},
    {
      path: '/',
      // name:'index',
      component: () => import('./views/Index.vue'),
      children:[
        {
          path:'',
          redirect:'/home'
        },
        {
          path:'/home',
          name:'Home',
          component: () => import('./views/Home.vue')
        },
        {
          path:'/order', 
          name:'Order',
          component: () => import('./views/Order.vue')
        },
        {
          path:'/mine',
          name:'Mine',
          component: () => import('./views/Mine.vue')
        },
        {
          path:'/address',
          name:'Address',
          component: () => import('./views/Address.vue')
        }
      ]
    }
  ]
})

// 路由守卫 beforeEach( () => {})
router.beforeEach((to,from,next)=>{
  // 判断islogin 登录状态true or false
  const islogin = localStorage.ele_login ? true : false
  // 如果路径to.path为/login，则继续进行，否则跳转到/login  (to.path为跳转的路径<router-link to="/login">)
  if (to.path == '/login') {
    next()
  } else {
    // 判断是否为登录状态，否则跳转到登录页面
    islogin ? next() : next('/login')
  }
  // 结果:localhost:8080/ + 任何字符 =》 跳转到登录页面
})


export default router
